package com.xh.core.util;

import java.io.File;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Component
public class EmailUtils {

	private static JavaMailSender javaMailSender;
	@Autowired
	private JavaMailSender javaMailSenderMapping;

	@PostConstruct
	public void init() {
		javaMailSender = javaMailSenderMapping;
	}

	public static void main(String[] args) {
		javaMailSender = javaMailSender();
		System.out.println(sendEnclosureEmail("爱学每日线索", "dasdfasd", "40087783@qq.com", "daiwei@xiaohe.com,daiwei@xiaohe.com",
				"测试文件.docx","/Users/david/Documents/工作/小禾/运维工单.docx"));
	}

	public static JavaMailSender javaMailSender() {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		final Properties p = System.getProperties() ;
//		p.setProperty("mail.smtp.host", "smtp.qq.com");
		p.setProperty("mail.smtp.auth", "true");
//		p.setProperty("mail.smtp.user", "40087783@qq.com");
//		p.setProperty("mail.smtp.pass", "yqcwbssscqkrbjeh");

		p.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
		p.setProperty("mail.smtp.socketFactory.fallback", "false");
		//邮箱发送服务器端口,这里设置为465端口
		p.setProperty("mail.smtp.port", "465");
		p.setProperty("mail.smtp.socketFactory.port", "465");


		JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
		javaMailSender.setJavaMailProperties(p);
		javaMailSender.setHost("smtp.qq.com");
//		javaMailSender.setPort(25);
		javaMailSender.setUsername("40087783@qq.com");
		javaMailSender.setPassword("yqcwbssscqkrbjeh");
		return javaMailSender;
	}

/**
	 * 发送纯文本文件
	 * 
	 * @param title
	 *            标题
	 * @param content
	 *            内容
	 * @param from
	 *            发送邮箱
	 * @param to
	 *            接收邮箱
	 * @return
	 */

	public static boolean sendSimpleEmail(String title, String content, String from, String to) {
		// 构造Email消息
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(from);
		message.setTo(to.split(","));
		message.setSubject(title);
		message.setText(content);
		try {
			javaMailSender.send(message);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

/**
	 * 发送单个附件邮件
	 * 
	 * @param title
	 *            标题
	 * @param content
	 *            内容
	 * @param from
	 *            发送邮箱
	 * @param to
	 *            接收邮箱
	 * @param resMap
	 * @return
	 * @throws MessagingException
	 */

	public static boolean sendEnclosureEmail(String title, String content, String from, String to,
			Map<String, String> resMap) {
		// MimeMessage 本身的 API 有些笨重，我们可以使用 MimeMessageHelper
		MimeMessage mimeMessage = javaMailSender.createMimeMessage();
		try {
			// 第二个参数是 true ，表明这个消息是 multipart类型的/
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
			mimeMessageHelper.setFrom(from);
			mimeMessageHelper.setTo(to.split(","));
			mimeMessageHelper.setSubject(title);
			mimeMessageHelper.setText(content);
			for (Map.Entry<String, String> res : resMap.entrySet()) {
				// 添加附件,第一个参数表示添加到 Email 中附件的名称，第二个参数是图片资源
				mimeMessageHelper.addAttachment(res.getKey(), new ClassPathResource(res.getValue()));
			}
			javaMailSender.send(mimeMessage);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

/**
	 * 发送单个附件邮件
	 * 
	 * @param title
	 *            标题
	 * @param content
	 *            内容
	 * @param from
	 *            发送邮箱
	 * @param to
	 *            接收邮箱
	 * @param filePath
	 * @return
	 * @throws MessagingException
	 */

	public static boolean sendEnclosureEmail(String title, String content, String from, String to, String fileName,
			String filePath) {
		// MimeMessage 本身的 API 有些笨重，我们可以使用 MimeMessageHelper
		MimeMessage mimeMessage = javaMailSender.createMimeMessage();
		try {
			// 第二个参数是 true ，表明这个消息是 multipart类型的/
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
			mimeMessageHelper.setFrom(from);
			mimeMessageHelper.setTo(to.split(","));
			mimeMessageHelper.setSubject(title);
			mimeMessageHelper.setText(content);
			// 添加附件,第一个参数表示添加到 Email 中附件的名称，第二个参数是图片资源
			mimeMessageHelper.addAttachment(fileName, new File(filePath));
			javaMailSender.send(mimeMessage);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

     /**
	 * 
	 * @param title
	 *            标题
	 * @param content
	 *            内容
	 * @param from
	 *            发送邮箱
	 * @param to
	 *            接收邮箱
	 * @param resMap
	 * @return
	 */

	public static boolean sendHtmlEmail(String title, String content, String from, String to,
			Map<String, String> resMap) {
		MimeMessage mimeMessage = javaMailSender.createMimeMessage();
		try {
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
			mimeMessageHelper.setFrom(from);
			mimeMessageHelper.setTo(to.split(","));
			mimeMessageHelper.setSubject(title);
			mimeMessageHelper.setText(content, true);
			// 设置内嵌元素 cid，第一个参数表示内联图片的标识符，第二个参数标识资源引用
			for (Map.Entry<String, String> res : resMap.entrySet()) {
				mimeMessageHelper.addInline(res.getKey(), new ClassPathResource(res.getValue()));
			}
			javaMailSender.send(mimeMessage);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}